﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using mediatheque.Areas.Admin.Models;

namespace mediatheque.Areas.Admin.Controllers
{
    [Authorize]
    public class MembersController : Controller
    {
        private mediathequeEntities db = new mediathequeEntities();
        protected Members myMember = new Members();

        //
        // GET: /Admin/Users/

        public ActionResult Index()
        {
            return View(myMember);
        }

        //
        // GET: /Admin/Users/Details/5

        public ActionResult Details(int id)
        {
            if (myMember.getMemberById(id) == null)
                return View("NotFound");
            else
                return View(myMember);
        }

        //
        // GET: /Admin/Users/Create

        public ActionResult Create()
        {
            user u = new user();
            return View(u);
        } 

        //
        // POST: /Admin/Users/Create

        [HttpPost]
        public ActionResult Create(FormCollection collection)
        {
            try
            {
                user u = new user
                {
                    username = Request.Form["username"],
                    password = Request.Form["password"],
                    email = Request.Form["email"],
                    
                };

                if (TryUpdateModel(u))
                {
                    db.users.AddObject(u);
                    db.SaveChanges();
                }
                return RedirectToAction("Details", new { id = u.id });
            }
            catch
            {
                return View();
            }
        }
        
        //
        // GET: /Admin/Users/Edit/5
 
        public ActionResult Edit(int id)
        {
            if (myMember.getMemberById(id) == null)
                return View("NotFound");
            else
                return View(myMember);
        }

        //
        // POST: /Admin/Users/Edit/5

        [HttpPost]
        public ActionResult Edit(int id, FormCollection data)
        {
            var u = db.users.First(i => i.id == id);
            try
            {
                if (u == null)
                    return View("NotFound");
                else
                {
                    u.username = data["username"];
                    u.email = data["email"];
                    u.role_id = int.Parse(data["role"]);
                    db.SaveChanges();
                }
                return RedirectToAction("Edit", new { id = u.id });
            }
            catch
            {
                return RedirectToAction("Edit", new { id = u.id });
            }
        }

        //
        // POST: /Admin/Users/Delete/5
        public ActionResult Delete(int id, FormCollection collection)
        {
            var m = myMember.users.First(i => i.id == id);
            myMember.users.DeleteObject(m);
            myMember.SaveChanges();
            return RedirectToAction("Index");
        }

        public ActionResult Emprunt()
        {
            return View(myMember);
        }

        public ActionResult Historique()
        {
            return View(myMember);
        }
    }
}
